package com.glorystone.ins.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.glorystone.ins.domain.SysUser;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.glorystone.ins.common.common.PageVo;
import com.glorystone.ins.dao.SysAuthMapper;
import com.glorystone.ins.dao.SysRoleMapper;
import com.glorystone.ins.domain.SysRole;
import com.glorystone.ins.interceptor.PageList;
import com.glorystone.ins.service.ISysRoleService;

/**
 * Created by klaus on 9/27/16.
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class SysRoleServiceImpl  implements ISysRoleService {

	@Autowired
	private SysRoleMapper sysRoleMapper;


	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public boolean addRole(SysRole role) {
		int i = sysRoleMapper.insert(role);
		return i==0 ? false : true;
	}

	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public boolean delRole(Long roleId) {
		SysRole role = new SysRole();
		role.setTid(roleId);
		role.setDelFlag(1);
		int i = sysRoleMapper.update(role);
		return i==0 ? false : true;
	}

	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public boolean updateRole(SysRole role) {
		int i = sysRoleMapper.update(role);
		return i==0 ? false : true;
	}

	@Override
	public SysRole getRoleById(Long roleId) {
		return sysRoleMapper.selectByPrimaryKey(roleId);
	}

	@Override
	public PageList<SysRole>  queryAllRoleList(PageVo vo) {
		return sysRoleMapper.queryAllRoleList(vo, vo.getPageable());
	}

	@Override
	public List<SysRole> checkRoleByName(String name) {
		return sysRoleMapper.checkRoleByName(name);
	}

	@Override
	public List<Map<String, Object>> getAllDepartment() {
		return sysRoleMapper.getAllDepartment();
	}

	@Override
	public List<Map<String, Object>> getAllInOutDepartment() {
		return sysRoleMapper.getAllInOutDepartment();
	}

	@Override
	public List<Map<String, String>> getRoleType(Long tid) {
		return sysRoleMapper.getRoleType(tid);
	}

	@Override
	public List<SysRole> getAllRoleList() {
		return sysRoleMapper.getAllRoleList();
	}

	@Override
	public List<SysRole> getRoleListByDepartmentId(Long departmentId) {
		return sysRoleMapper.getRoleListByDepartmentId(departmentId);
	}

	/*@Override
	public List<SysUser> getUserListByRoleType(Long roleTypeId) {
		return sysRoleMapper.getUserListByRoleType(roleTypeId);
	}
	*/
	@Override
	public List<SysUser> getUserListByDepartmentId(Long departmentId) {
		List<SysUser> users = sysRoleMapper.getUserListByDepartmentId(departmentId);
		List<SysRole> usRoles = sysRoleMapper.getUselessRoles();
		String usRoleArr = "0,";
		List<SysUser> userList = Lists.newArrayList();
		for(SysRole role : usRoles){
			usRoleArr += role.getTid()+",";

		}
		for(SysUser user : users){
			String roleIds = user.getRoleId();
			String[] roIds = roleIds.split(",");
			boolean use = false;
			for(int i=0;i<roIds.length;i++){
				if(usRoleArr.indexOf(roIds[i])<0){
					use = true;
				}
				if(use){
					userList.add(user);
					break;
				}
			}

		}
		return userList;
	}
}
